package com.salon.client.widgets.panels;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.form.LabelField;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.SuggestOracle.Suggestion;
import com.salon.client.GreetingServiceAsync;
import com.salon.client.Salon;
import com.salon.client.beans.IncomeMaterialsBean;
import com.salon.client.beans.MaterialBean;
import com.salon.client.tabItems.IncomeTabItemPanel;
import com.salon.client.widgets.IncomeMaterialDialog;
import com.salon.client.widgets.MaterialDialog;
import com.salon.client.widgets.suggest.MaterialSuggest;
import com.salon.client.widgets.suggest.ParentSuggestion;

public class IncomeMaterialsPanel extends FlexTable {
	protected IncomeMaterialsBean bean = new IncomeMaterialsBean();
	private IncomeTabItemPanel parentPanel;
	private SuggestBox name = null;
	protected TextField<String> desc = new TextField<String>();
	protected NumberField cost = new NumberField();
	protected NumberField count = new NumberField();
	
	private Button ok = new Button("Ок");
	private Button cancel = new Button("Отмена");
	private PopupPanel popupPanel;
	
	private MaterialBean selectedMaterial = null;
	public IncomeMaterialsPanel(IncomeTabItemPanel panel, PopupPanel popupPanel, int incomeId) {
		this.parentPanel = panel;
		this.popupPanel = popupPanel;
		bean.setIncomeId(incomeId);
		setCellSpacing(4);
		setCellPadding(5);
		setStylePrimaryName("brit-main-view");
		int row = 0;
		
		setWidget(row, 0, new HTML("<font color=darkgreen><center><h2>Добавление товара</h2></center></font>"));
		getFlexCellFormatter().setColSpan(row, 0, 2);
		getFlexCellFormatter().setHorizontalAlignment(row, 0, HasHorizontalAlignment.ALIGN_CENTER);
		
		
		LabelField lbl = new LabelField("Наименование:");
		lbl.setWidth("150px");
		setWidget(++row, 0, lbl);
		getFlexCellFormatter().setWidth(row, 0, "150px");
		getFlexCellFormatter().setHorizontalAlignment(row, 0, 
				HasHorizontalAlignment.ALIGN_RIGHT);
		
		setWidget(row, 1, createNameField());
		getFlexCellFormatter().setWidth(row, 1, "90%");
		getFlexCellFormatter().setHorizontalAlignment(row, 1, 
				HasHorizontalAlignment.ALIGN_LEFT);
		
		lbl = new LabelField("Описание:");
		lbl.setWidth("150px");
		setWidget(++row, 0, lbl);
		getFlexCellFormatter().setWidth(row, 0, "270px");
		getFlexCellFormatter().setHorizontalAlignment(row, 0, 
				HasHorizontalAlignment.ALIGN_RIGHT);
		desc.setWidth(270);
		setWidget(row, 1, desc);
		
		lbl = new LabelField("Цена:");
		lbl.setWidth("150px");
		setWidget(++row, 0, lbl);
		getFlexCellFormatter().setWidth(row, 0, "270px");
		getFlexCellFormatter().setHorizontalAlignment(row, 0, 
				HasHorizontalAlignment.ALIGN_RIGHT);
		cost.setWidth(270);
		setWidget(row, 1, cost);
		
		lbl = new LabelField("Количество:");
		lbl.setWidth("150px");
		setWidget(++row, 0, lbl);
		getFlexCellFormatter().setWidth(row, 0, "150px");
		getFlexCellFormatter().setHorizontalAlignment(row, 0, 
				HasHorizontalAlignment.ALIGN_RIGHT);
		count.setWidth(270);
		setWidget(row, 1, count);
		
		
		HorizontalPanel buttons = new HorizontalPanel();
		buttons.setSpacing(10);
		
		ok.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (!getData()) {
					return;
				}
				((GreetingServiceAsync) Registry.get(Salon.SERVICE)).
				addIncomeMaterial(bean, new AsyncCallback<IncomeMaterialsBean>() {

					@Override
					public void onFailure(Throwable caught) {
						Info.display("Не удалось", "Добавить товар");
						
					}

					@Override
					public void onSuccess(IncomeMaterialsBean result) {
						Info.display("Добавление товара", "Успешно");
						IncomeMaterialsPanel.this.popupPanel.hide();
						IncomeMaterialsPanel.this.parentPanel.refreshStore();
					}
					
				});
			}
		});
		ok.setWidth("110px");

		cancel.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				IncomeMaterialsPanel.this.popupPanel.hide();
			}
		});
		buttons.add(ok);
		
		cancel.setWidth("110px");
		buttons.add(cancel);
		
		setWidget(++row, 0, buttons);
		getFlexCellFormatter().setColSpan(row, 0, 2);
		getFlexCellFormatter().setHorizontalAlignment(row, 0, HasHorizontalAlignment.ALIGN_CENTER);
		
	}
	
	private FlexTable createNameField() {
		name = new SuggestBox(new MaterialSuggest(MaterialSuggest.FIELD.NAME));
		name.setFocus(true);
		name.setWidth("270px");
		
		name.setLimit(MaterialSuggest.MAXSUGGESTIONS);
		name.setAutoSelectEnabled(true);
		SelectionHandler<Suggestion> sel = new SelectionHandler<Suggestion>() {
			public void onSelection(SelectionEvent<Suggestion> event) {
				selectedMaterial = (MaterialBean)((ParentSuggestion)event.getSelectedItem()).getBean();
				name.setValue(selectedMaterial.getDisplayF());
				name.getTextBox().setEnabled(false);
				cost.setValue(selectedMaterial.getCost());
				
			}};
		name.addSelectionHandler(sel);
		PushButton btnClearLName = new PushButton(new Image("salon/icons/glyphlab/prevseries.png"),
			new ClickHandler() {
				public void onClick(ClickEvent event) {
					name.setValue(null);
					name.getTextBox().setEnabled(true);
					name.setFocus(true);
					selectedMaterial = null;
			}});
		
		btnClearLName.setStylePrimaryName("brit-transparent");
		btnClearLName.setTitle("Очистить");
		btnClearLName.setSize("16px", "16px");
		
		PushButton addMaterial = new PushButton(new Image("salon/icons/glyphlab/add.gif"),
			new ClickHandler() {
				public void onClick(ClickEvent event) {
					MaterialDialog dlg = new MaterialDialog(null, null);
					dlg.show();
			}});
			
		addMaterial.setStylePrimaryName("brit-transparent");
		addMaterial.setTitle("Добавить материал");
		addMaterial.setSize("16px", "16px");
		
		FlexTable ftLName = new FlexTable();
		ftLName.setWidget(0, 0, name);
		ftLName.getFlexCellFormatter().setWidth(0, 0, "100%");
		ftLName.setWidget(0, 1, btnClearLName);
		ftLName.getFlexCellFormatter().setWidth(0, 1, "20px");
		ftLName.setWidget(0, 2, addMaterial);
		ftLName.getFlexCellFormatter().setWidth(0, 2, "20px");
		ftLName.setWidth(270 + "px");
		return ftLName;
	}
	
	private boolean getData() {
		if (selectedMaterial != null) {
			bean.setMaterialId(selectedMaterial.getId());
		}
		else {
			Info.display("Выберите", "материал");
			name.setFocus(true);
			return false;
		}
		
		bean.setDesc(desc.getValue());
		
		if (cost.getValue() != null) {
			bean.setCost(cost.getValue().floatValue());
		}
		else {
			Info.display("Выберите", "Цену");
			cost.focus();
			return false;
		}
		if (count.getValue() != null) {
			bean.setCount(count.getValue().intValue());
		}
		else {
			Info.display("Выберите", "количество");
			count.focus();
			return false;
		}
		return true;
	}
}